package com.github.ecbp.user.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.ecbp.common.constant.ConstantCode;
import com.github.ecbp.common.data.service.impl.BaseServiceImpl;
import com.github.ecbp.user.service.entity.TPermission;
import com.github.ecbp.user.service.entity.TRole;
import com.github.ecbp.user.service.entity.TRolePermissionRelation;
import com.github.ecbp.user.service.mapper.RolePermissionRelationMapper;
import com.github.ecbp.user.service.service.RolePermissionRelationService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class RolePermissionRelationServiceImpl extends BaseServiceImpl<RolePermissionRelationMapper, TRolePermissionRelation> implements RolePermissionRelationService {

    @Override
    public List<TPermission> getPermissionByRoleId(Long roleId) {
        QueryWrapper<TRolePermissionRelation> tRolePermissionRelationQueryWrapper = new QueryWrapper<>();
        tRolePermissionRelationQueryWrapper.select("distinct p.id", "p.name", "p.power");
        tRolePermissionRelationQueryWrapper.eq("rr.role_id", roleId);
        tRolePermissionRelationQueryWrapper.eq("p.status", ConstantCode.TRUE);
        tRolePermissionRelationQueryWrapper.eq("p.delete_status", ConstantCode.TRUE);
        tRolePermissionRelationQueryWrapper.eq("rr.delete_status", ConstantCode.TRUE);
        return baseMapper.getPermissionByRoleId(tRolePermissionRelationQueryWrapper);
    }

    @Override
    public List<TPermission> getPermissionByRoleId(Set<Long> roleIds) {
        QueryWrapper<TRolePermissionRelation> tRolePermissionRelationQueryWrapper = new QueryWrapper<>();
        tRolePermissionRelationQueryWrapper.select("distinct p.id", "p.name", "p.power");
        tRolePermissionRelationQueryWrapper.in("rr.role_id", roleIds);
        tRolePermissionRelationQueryWrapper.eq("p.status", ConstantCode.TRUE);
        tRolePermissionRelationQueryWrapper.eq("p.delete_status", ConstantCode.TRUE);
        tRolePermissionRelationQueryWrapper.eq("rr.delete_status", ConstantCode.TRUE);
        return baseMapper.getPermissionByRoleId(tRolePermissionRelationQueryWrapper);
    }

    @Override
    public List<TRole> getRoleByPermissionId(Long permissionId) {
        QueryWrapper<TRolePermissionRelation> tRolePermissionRelationQueryWrapper = new QueryWrapper<>();
        tRolePermissionRelationQueryWrapper.select("distinct r.id", "r.name");
        tRolePermissionRelationQueryWrapper.eq("rr.permission_id", permissionId);
        tRolePermissionRelationQueryWrapper.eq("r.status", ConstantCode.TRUE);
        tRolePermissionRelationQueryWrapper.eq("r.delete_status", ConstantCode.TRUE);
        tRolePermissionRelationQueryWrapper.eq("rr.delete_status", ConstantCode.TRUE);
        return baseMapper.getRoleByPermissionId(tRolePermissionRelationQueryWrapper);
    }

    @Override
    public List<TRole> getRoleByPermissionId(Set<Long> permissionIds) {
        QueryWrapper<TRolePermissionRelation> tRolePermissionRelationQueryWrapper = new QueryWrapper<>();
        tRolePermissionRelationQueryWrapper.select("distinct r.id", "r.name");
        tRolePermissionRelationQueryWrapper.in("rr.permission_id", permissionIds);
        tRolePermissionRelationQueryWrapper.eq("r.status", ConstantCode.TRUE);
        tRolePermissionRelationQueryWrapper.eq("r.delete_status", ConstantCode.TRUE);
        tRolePermissionRelationQueryWrapper.eq("rr.delete_status", ConstantCode.TRUE);
        return baseMapper.getRoleByPermissionId(tRolePermissionRelationQueryWrapper);
    }
}
